package com.thomas.shaimn;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.sun.org.apache.bcel.internal.generic.GETSTATIC;
import com.thomas.model.ImageModel;
import com.thomas.tool.ImageUtil;

/**
 * 从 www.shaimn.com 下载图片
 * @author thomas
 *
 */
public class DownloadImagesFromShaimn {
	
	private static final String SITE_NAME = "shaimn";
	private static final int FOLDER_COUNT = 100;
	
	/**
	 * 根据类别保存图片
	 * @param imageExatId
	 * @return
	 */
	public static boolean saveImageByCategoryId(String imageExatId) {

		// http://www.shaimn.com/view/1121.html
		// http://www.shaimn.com/view/1555.html
		// http://www.shaimn.com/view/1385.html
		// http://www.shaimn.com/view/3187.html
		// http://www.shaimn.com/view/2277.html
		// http://www.shaimn.com/view/1170.html

		String siteBaseUrl  = "http://www.shaimn.com";
		String pageUrl = siteBaseUrl + "/view/" + imageExatId + ".html";
		
		try {
			String userAgent = "Chrome/45.0.2454.81 Safari/537.36";
			Document doc = Jsoup.connect(pageUrl).timeout(36000).userAgent(userAgent).get();
			String title = doc.title().split("_")[0];
			String saveName = imageExatId + "_" + title + ".jpg";
			String imageUrl = siteBaseUrl + doc.select(".viewpic>img").attr("src");
			
			
			int categoryStartId = getStartCategoryId(imageExatId);
			int categoryEndid = getEndCategoryid(imageExatId);
			
			// 将参数封装到一个对象中去
			ImageModel imageModel = new ImageModel();
			imageModel.setImageUrl(imageUrl);
			imageModel.setImageName(saveName);
			imageModel.setImageFolder(SITE_NAME + "_" + categoryStartId + "_" + categoryEndid);
			
			return ImageUtil.saveImageByImageModel(imageModel);
		} catch (Exception e) {
			System.out.println(imageExatId + " time out!");
			return false;
		}

	}
	
	/**
	 * 得到类别开始的数字
	 * @param s
	 * @return
	 */
	public static int getStartCategoryId(String s) {
		String[] imageExatArr = s.split("_");
		int categoryId = Integer.parseInt(imageExatArr[0]);
		return ((int)(categoryId / FOLDER_COUNT))*FOLDER_COUNT;
	}
	
	/**
	 * 得到类别结束的数字
	 * @param s
	 * @return
	 */
	public static int getEndCategoryid(String s) {
		int categoryStart = getStartCategoryId(s);
		return categoryStart + FOLDER_COUNT;
	}
	
	/**
	 * 保存图片线程
	 * @param i 类别ID
	 */
	public static void save(int i) {
		
		boolean isSuccess = DownloadImagesFromShaimn.saveImageByCategoryId(i+"");
		
		if (isSuccess != true) {
			System.out.println("skip "+i+" ...");
			return;
		}
		
		for (int k=2; k<99; k++) {
			new SaveImageThreadTool(i+"_"+k).start();
		}
	}
	
	/**
	 * 执行的主函数
	 * @param args
	 */
	public static void main(String[] args) {
		
		// min = 44
		// cur = 955
		// max = 9999
		for (int i=4580; i<9999; i++) {
			DownloadImagesFromShaimn.save(i);
		}
	}
}


//保存图片线程
class SaveImageThreadTool extends Thread {
	
	private String saveCateId = "";

	public SaveImageThreadTool(String saveCateId) {
		super();
		this.saveCateId = saveCateId;
	}

	public void run() {
		DownloadImagesFromShaimn.saveImageByCategoryId(this.saveCateId);
	}
	
}
